package com.ujiuye.servlet;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.mchange.v2.beans.BeansUtils;
import com.ujiuye.entity.Course;
import com.ujiuye.entity.Coursedetail;
import com.ujiuye.entity.ResultVO;
import com.ujiuye.service.CourseService;
import com.ujiuye.service.impl.CourseServiceImpl;
import com.ujiuye.util.JsonUtils;
import com.ujiuye.util.PageBean;
import com.ujiuye.util.UploadFile;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * CompanyName: IT优就业<br/>
 * Datetime: 2021/7/2 10:33<br/>
 *
 * @author TanJian
 */
@WebServlet("/course")
@MultipartConfig
public class CourseServlet extends  BaseServlet {


    CourseService cs = new CourseServiceImpl();

    /**
     * 上传文件
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void uploadFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1:获取上传的文件
        Part part = req.getPart("file");
        //2:获取文件名
        String filename = UploadFile.upload(part);

        //3：拼接图片地址
        String url = "http://127.0.0.1:8080/img/"+filename;

        //4：给出响应
        ResultVO vo = null;
        if(filename.contains("jpg") || filename.contains("png") || filename.contains("gif")){
            vo = new ResultVO(200,"上传图片成功",url);
        }else{
            vo = new ResultVO(200,"上传视频成功",url);
        }

        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);


    }


    /**
     * 删除文件
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void removeFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1:获取需要删除的地址
        String fileName = req.getParameter("fileName");
        //截取url最后一部分
        fileName = fileName.substring(fileName.lastIndexOf("/")+1);
        //删除资源
        File file = new File("D:\\A学习文件\\Java二阶段内容\\Java内容\\7月2\\图片\\"+fileName);
        ResultVO vo = null;
        if(file.exists()){
            file.delete();
            if (fileName.contains("jpg") || fileName.contains("png") || fileName.contains("gif")) {
                vo = new ResultVO(200, "删除图片成功", 1);
            } else {
                vo = new ResultVO(200, "删除视频成功", 2);
            }
        }
        //响应
        resp.getWriter().print(JsonUtils.toJson(vo,resp));


    }


    /**
     * 上传课程
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addCourse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //1:获取前端传来的课程信息
        Map<String, String[]> map = req.getParameterMap();
        Course course = new Course();
        BeanUtils.populate(course,map);
        System.out.println(course);

        String courseImage = course.getCourseImage();
        String courseVideo = course.getCourseVideo();
        course.setCourseImage(courseImage.substring(courseImage.lastIndexOf("/")+1));
        course.setCourseVideo(courseVideo.substring(courseVideo.lastIndexOf("/")+1));
        course.setCreateTime(new Date().toLocaleString());
        //2 调用service
        int num = cs.addCourse(course);

        //3:给出响应
        ResultVO vo = null;
        if(num>0){
            vo = new ResultVO(200,"添加课程成功",null);
        }else{
            vo = new ResultVO(500,"添加课程失败",null);
        }

        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);


    }

    /**
     * 分页查询课程
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //1:获取前端传来的分页信息
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String courseName = req.getParameter("courseName");
        //2: 调用业务组装PageBean
        PageBean pb = cs.getPageBean(currentPage,pageSize,courseName);
        //3:给出响应
        String json = JsonUtils.toJson(pb, resp);

        resp.getWriter().print(json);


    }
    /**
     *   批量删除
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //1:获取前端传来的cids
        String cids = req.getParameter("cids");
        //2:调用service
        int num = cs.delAll(cids);
        //3:给出响应
        ResultVO vo = null;
        if(num>0){
            vo = new ResultVO(200,"删除成功",null);
        }else{
            vo = new ResultVO(500,"删除失败",null);
        }

        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);


    }
    /**
     *   批量删除
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCourse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //1:获取前端课程信息
        Map<String, String[]> map = req.getParameterMap();
        //2:创建课程对象
        Course course = new Course();
        BeanUtils.populate(course,map);
        //3:获取图片和视频
        String courseImage = req.getParameter("courseImage1");
        String courseVideo = req.getParameter("courseVideo1");
        //4:截取图片和视频
        course.setCourseImage(courseImage.substring(courseImage.lastIndexOf("/")+1));
        course.setCourseVideo(courseVideo.substring(courseVideo.lastIndexOf("/")+1));
        //5:"调用service
        int num = cs.updateCourse(course);

        //6:给出响应
        ResultVO vo = null;
        if(num>0){
            vo = new ResultVO(200,"修改成功",null);
        }else{
            vo = new ResultVO(500,"修改失败",null);
        }
        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);

    }
    /**
     *   课程明细
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addCourseDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //1:获取前端课程明细信息
        Map<String, String[]> map = req.getParameterMap();
        //2:创建课程明细对象
        Coursedetail cd = new Coursedetail();
        BeanUtils.populate(cd,map);
        String type = cd.getType();
        //处理章节
        if("1".equals(type)){
            cd.setType("第一章");
        }else if("2".equals(type)){
            cd.setType("第二章");
        }else{
            cd.setType("第三章");
        }
        //处理视频地址
        cd.setUrl(cd.getUrl().substring(cd.getUrl().lastIndexOf("/")+1));


        //3:调用service
        int num =  cs.addCourseDetail(cd);
        //4：给出响应
        ResultVO vo = null;
        if(num>0){
            vo = new ResultVO(200,"添加课程明细成功",null);
        }else{
            vo = new ResultVO(500,"添加课程明细失败",null);
        }

        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);


    }

    /**
     *   查询所有课程
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findCourseByType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //1:获取前端课程信息
        int courseType =Integer.parseInt(req.getParameter("courseType"));

        //2:"调用service
        List<Course> list = cs.findCourseByType(courseType);

        //3:给出响应
        String json = JsonUtils.toJson(list, resp);
        resp.getWriter().print(json);

    }
    /**
     * 前端分页查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findAllSelect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //1:获取前端传来的分页信息
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String courseName = req.getParameter("courseName");
        String courseType = req.getParameter("courseType");
        //2: 调用业务组装PageBean
        PageBean<Course> pb = cs.findAllSelect(currentPage,pageSize,courseName,courseType);
        //3:给出响应
        String json = JsonUtils.toJson(pb, resp);

        resp.getWriter().print(json);


    }


    /**
     * 前端分页查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findCourse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //1:获取前端cid
        int cid =Integer.parseInt(req.getParameter("cid")) ;

        //2: 调用业务组装
        Course course= cs.findCourse(cid);
        //3:给出响应
        String json = JsonUtils.toJson(course, resp);

        resp.getWriter().print(json);


    }

    /**
     * 前端分页查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findCourseDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //1:获取前端cid
        int cid =Integer.parseInt(req.getParameter("cid")) ;

        //2: 调用业务组装
        Map<String,List<Coursedetail>> map= cs.findCourseDetail(cid);
        //3:给出响应
        String json = JsonUtils.toJson(map, resp);

        resp.getWriter().print(json);


    }

    /**
     * 根据uid查询所购买的课程
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findCourseByUid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //1:获取前端uid
        int uid =Integer.parseInt(req.getParameter("uid")) ;

        //2: 调用业务组装
        List<Course> list= cs.findCourseByUid(uid);
        //3:给出响应
        String json = JsonUtils.toJson(list, resp);

        resp.getWriter().print(json);


    }

}


